package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BizLogic.MonthlyUsageManager;

import model.BookingDTO;



/**
 * Servlet implementation class LoadMonthlyUsage
 */
@WebServlet("/LoadMonthlyUsage")
public class LoadMonthlyUsage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoadMonthlyUsage() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	public static String month="January";
	
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		if(request.getParameter("Month")!=null){
			month = request.getParameter("Month");
		}
		String monthString;
		switch (month) {
		case "January":
			monthString = "01";
			break;
		case "February":
			monthString = "02";
			break;
		case "March":
			monthString = "03";
			break;
		case "April":
			monthString = "04";
			break;
		case "May":
			monthString = "05";
			break;
		case "June":
			monthString = "06";
			break;
		case "July":
			monthString = "07";
			break;
		case "August":
			monthString = "08";
			break;
		case "September":
			monthString = "09";
			break;
		case "October":
			monthString = "10";
			break;
		case "November":
			monthString = "11";
			break;
		case "December":
			monthString = "12";
			break;
		default:
			monthString="01";
			break;

		}
		String fromDate="2012-"+monthString+"-01";
		String toDate="2012-"+monthString+"-31";
		DateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date frDate=null;
		java.util.Date tDate=null;		
		try {
			frDate=dateformat.parse(fromDate);
			tDate=dateformat.parse(toDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date setFromDate=new java.sql.Date(frDate.getTime());
		java.sql.Date setToDate=new java.sql.Date(tDate.getTime());
		MonthlyUsageManager monthlyManager = new MonthlyUsageManager();
		ArrayList<BookingDTO> allBooking = monthlyManager.MonthlyUsage(setFromDate, setToDate);
		request.setAttribute("allbooking", allBooking);
		HttpSession session=request.getSession(true);
		session.setAttribute("Print", allBooking);

		/*for(BookingDTO b:allBooking){
			b.getFacilityName();
			b.getFacilityType().getFacilityType();
			b.getDuration();
		}*/
		RequestDispatcher rd = request
				.getRequestDispatcher("ViewMonthlyUsage.jsp");
		rd.forward(request, response);
	}

}
